package org.foreverframework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FileUtil {

	public static void saveFile(File src, File target) throws Exception {
		FileInputStream in = new FileInputStream(src);
		FileOutputStream out = new FileOutputStream(target);
		byte[] buffer = new byte[1024];
		int bytes_read = 0;
		while ((bytes_read = in.read(buffer)) != -1) {
			out.write(buffer, 0, bytes_read);
		}
		in.close();
		out.close();
	}

	public static void saveFile(InputStream in, String savePath, String fileName)
			throws Exception {
		if (!new File(savePath).exists())
			new File(savePath).mkdirs();
		FileOutputStream out = new FileOutputStream(savePath + fileName);
		byte[] buffer = new byte[1024];
		int bytes_read = 0;
		while ((bytes_read = in.read(buffer)) != -1) {
			out.write(buffer, 0, bytes_read);
		}
		in.close();
		out.close();
	}

	public static void saveFile(String src, String target) throws Exception {
		saveFile(new File(src), new File(target));
	}

	public static void deleteFile(String path) {
		File target = new File(path);
		if (target.exists())
			target.delete();
	}
}
